package cn.audit.controller.operation;

import cn.audit.aop.myannotation.BussAnnotation;
import cn.audit.bean.dto.UserCorrelation;
import cn.audit.bean.pojo.*;
import cn.audit.service.institution.InstitutionService;
import cn.audit.service.operation.ProjectsItemService;
import cn.audit.service.user.UserService;
import javassist.compiler.Parser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author fei人类
 * @date 2019/05/20
 */

@Controller
@RequestMapping("/Projects")
public class ProjectsItemController {

    @Resource
    private ProjectsItemService projectsItemService;

    @Resource
    private InstitutionService institutionService;

    @Resource
    private UserService userService;

    @RequestMapping(value = "/projectsIndex")
    public String index(Model model) {
        List<UserCorrelation> project = projectsItemService.findItemAll();
        model.addAttribute("p", project);
        return "operation/messageProjects";
    }

    @RequestMapping(value = "/liItemIndex")
    public String liItemIndex(Model model, Long id) {
        List<CheckItem> checkItems = projectsItemService.findCheckItemAll();
        List<ExtractionUnit> extractionUnits = projectsItemService.findUnitAll();
        List<AuditInstitution> institutions = institutionService.findAll();
        model.addAttribute("institutions", institutions);
        model.addAttribute("checkItems", checkItems);
        model.addAttribute("units", extractionUnits);
        if (id != null && id != 0) {
            UserCorrelation p = projectsItemService.findItemById(id);
            model.addAttribute("p", p);
            return "operation/standProjects1";
        } else {
            return "operation/standProjects";
        }


    }


    @RequestMapping(value = "/liItem")
    @BussAnnotation(moduleName = "项目", type = "编辑", option = "成功")
    public String liItem(Model model, HttpSession session,
                         Long id,
                         String name,
                         Long type,
                         Long phase,
                         String item,
                         String unit,
                         Long review,
                         String stime,
                         String etime) {

        if (unit != null && unit != "" && !unit.isEmpty()) {
            String[] units = unit.trim().split("unit=|&unit=|&");
            unit = "";
            for (String u : units) {
                if (u != null && u != "") {
                    unit += u + ",";
                }
            }
            if (unit.charAt(0) == ',') {
                unit = unit.substring(1, unit.length() - 1);
            } else {
                unit = unit.substring(0, unit.length() - 1);
            }
        }
        if (item != null && unit != "" && !unit.isEmpty()) {
            String[] items = item.trim().split("item=|&item=|&");
            item = "";
            for (String u : items) {
                if (u != null && u != "") {
                    item += u + ",";
                }
            }
            if (item.charAt(0) == ',') {
                item = item.substring(1, item.length() - 1);
            } else {
                item = item.substring(0, item.length() - 1);
            }

        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date stimedate = null;
        Date etimedate = null;
        try {
            if (stime != null && stime != "" && !stime.isEmpty()) {
                stimedate = sdf.parse(stime);
            }
            if (etime != null && etime != "" && !etime.isEmpty()) {
                etimedate = sdf.parse(etime);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        long status = 1;
        Project1 project = null;
        if (id != null && id != 0) {
            project = new Project1(id, name, type, phase,
                    item, unit, review, stimedate,
                    etimedate, status);
        } else {
            User user1 = (User) session.getAttribute("user");
            project = new Project1(name, type, phase,
                    item, unit, review, stimedate,
                    etimedate, status, new Date(), user1.getId());
        }

        Project1 p = projectsItemService.saveProject(project);
        List<UserCorrelation> users = projectsItemService.findUserAll();

        model.addAttribute("u", users);
        model.addAttribute("p", p);
        return "operation/projectsRole";
    }

    @RequestMapping(value = "/fpItem")
    @BussAnnotation(moduleName = "项目", type = "人员分配", option = "成功")
    public String fpItem(Model model,
                         Long id,
                         Long lord,
                         Long adjudication) {
        projectsItemService.updateProject(id, lord, adjudication);
        List<UserCorrelation> users = projectsItemService.findUserAll();
        List<Taskocation> tasks = projectsItemService.findTaskocationAll();
        List<Project1> projects = projectsItemService.findProject1All();

        model.addAttribute("users", users);
        model.addAttribute("tasks", tasks);
        model.addAttribute("items", projects);
        return "operation/fpTask";
    }

    /**
     * 审计方案
     */
    @RequestMapping(value = "/itemPro")
    public String itemPro(Model model,
                          Long id) {

        Project1 p = projectsItemService.findProjectsById(id);

        List<Long> userids = new ArrayList<>();
        userids.add(p.getLord());
        userids.add(p.getAdjudication());

        List<User> users = userService.findById(userids);

        if (p.getTaskoId() != null && p.getTaskoId() != "") {
            String task = p.getTaskoId();
            List<Long> tasks = new ArrayList<>();
            String[] ss = task.trim().split(",");
            for (String s : ss) {
                tasks.add(Long.parseLong(s));
            }
            List<UserCorrelation> task1 =
                    projectsItemService.findTaskAllById(tasks);
            model.addAttribute("t1", task1);
        }
        model.addAttribute("p", p);
        model.addAttribute("u", users);

        return "operation/auditpro";
    }

    /**
     * 提交审核
     */
    @RequestMapping(value = "/auditItemSubmit")
    @BussAnnotation(moduleName = "提交", type = "项目审核", option = "成功")
    public String auditItemSubmit(Model model,
                                  Long id) {
        projectsItemService.saveProject1(id);
        return "redirect:/Projects/projectsIndex";
    }

    /**
     * 提交审核
     */
    @RequestMapping(value = "/zzItem")
    @BussAnnotation(moduleName = "终止", type = "项目", option = "成功")
    public String zzItem(Model model,
                                  Long id) {
        projectsItemService.saveProject2(id);
        return "redirect:/Projects/projectsIndex";
    }

    @RequestMapping(value = "/fpItemAudit")
    @BussAnnotation(moduleName = "项目", type = "任务分配", option = "成功")
    @ResponseBody
    public List<Taskocation> fpAudit(
            Long itemId,
            Long userId,
            String ids) {

        if (ids != null) {
            ids = ids.substring(0, ids.length() - 1);
        }
        projectsItemService.updateItem(itemId, ids, userId);

        List<Taskocation> tasks = projectsItemService.findTaskocationAll();
        return tasks;
    }
}
